package com.cabinet.db;

import com.cabinet.Device;

import javax.swing.table.AbstractTableModel;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: denny
 * Date: 9/24/13
 * Time: 11:52 AM
 * To change this template use File | Settings | File Templates.
 */
@XmlRootElement(name = "class")
public class DBClass {
  String name = "";
  
  List<String> deviceNames = new ArrayList<>();
  List<DBConferenceCfg> conferenceConfigs = new ArrayList<>();
  ConferenceTabModel conferenceTabModel = new ConferenceTabModel();


  public DBClass() {
    for( Device dev : Device.all() ){
      deviceNames.add(dev.getName());
    }
  }

  public DBConferenceCfg getConfrenceConfig(int row){
    return conferenceConfigs.get(row);
  }


  @XmlAttribute(name = "class-name")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @XmlElement(name = "device-name")
  public List<String> getDeviceNames() {
    return deviceNames;
  }

  public void setDeviceNames(List<String> deviceNames) {
    this.deviceNames = deviceNames;
  }

  @XmlElement(name = "conference-config")
  public List<DBConferenceCfg> getConferenceConfigs() {
    return conferenceConfigs;
  }

  public void setConferenceConfigs(List<DBConferenceCfg> conferenceConfigs) {
    this.conferenceConfigs = conferenceConfigs;
  }

  public void addConferenceConfig(){
    DBConferenceCfg newCfg = new DBConferenceCfg();
    newCfg.setName("конфігурація "+(conferenceConfigs.size()+1));
    newCfg.initDevices();
    conferenceConfigs.add(newCfg);
    conferenceTabModel.fireTableRowsInserted(conferenceConfigs.size()-1, conferenceConfigs.size()-1);
  }


  public AbstractTableModel deviceNamesTabModel(){
    return new DeviceNamesTabModel();
  }

  public AbstractTableModel conferenceConfigsTabModel(){
    return conferenceTabModel;
  }

  class DeviceNamesTabModel extends AbstractTableModel {
    @Override
    public String getColumnName(int column) {
      return "Учні";
    }

    @Override
    public int getRowCount() {
      return deviceNames.size();
    }

    @Override
    public int getColumnCount() {
      return 1;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
      return deviceNames.get(rowIndex);
    }

    public boolean isCellEditable(int row, int column) {
      return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      deviceNames.set(rowIndex, (String) aValue);
    }
  }

  class ConferenceTabModel extends AbstractTableModel {
    @Override
    public String getColumnName(int column) {
      return "Конференції";
    }

    @Override
    public int getRowCount() {
      return conferenceConfigs.size();
    }

    @Override
    public int getColumnCount() {
      return 1;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
      return conferenceConfigs.get(rowIndex).getName();
    }

    public boolean isCellEditable(int row, int column) {
      return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      conferenceConfigs.get(rowIndex).setName((String) aValue);
    }
  }

}
